$(document).ready(function(){
    // $(document).ready() is executed after the page DOM id loaded
	
	
    // Binding an listener to the submit event on the form:
    $('#signupForm').submit(function(e){

        // If a previous submit is in progress:
        if($('#submit').hasClass('active')) return false;
		
        // Adding the active class to the button. Will show the preloader gif:
        $('#submit').addClass('active');
		
        // Removing the current error tooltips
        $('.errorTip').remove();
		
        // Issuing a POST ajax request to submit.php (the action attribute of the form):
        $.post($('#signupForm').attr('action'),$('#signupForm').serialize()+'&fromAjax=1',function(response){
			
            if(!response.status)
            {
                // Some kind of input error occured
				
                // Looping through all the input text boxes,
                // and checking whether they produced an error
                $('input[type!=submit]').each(function(){
                    var elem = $(this);
                    var id = elem.attr('id');
					
                    if(response[id])
                        showTooltip(elem,response[id]);
                });
            }
            else location.replace(response.redirectURL);
			
            $('#submit').removeClass('active');
            },'json');
		
        e.preventDefault();
            });
	
        $(window).resize();
	
        $("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)

        $("ul.topnav li span").click(function() { //When trigger is clicked...

            //Following events are applied to the subnav itself (moving subnav up and down)
            $(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

            $(this).parent().hover(function() {
                }, function(){
                $(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
                });

            //Following events are applied to the trigger (Hover events for the trigger)
            }).hover(function() {
            $(this).addClass("subhover"); //On hover over, add class "subhover"
            }, function(){	//On Hover Out
            $(this).removeClass("subhover"); //On hover out, remove class "subhover"
            });

        });

    // Centering the form vertically on every window resize:
    $(window).resize(function(){
        var cf = $('#carbonForm');
	
      /*  $('#carbonForm').css('margin-top',($(window).height()-cf.outerHeight())/2)
        });*/

    // Helper function that creates an error tooltip:
    function showTooltip(elem,txt)
    {
        // elem is the text box, txt is the error text
        $('<div class="errorTip">').html(txt).appendTo(elem.closest('.formRow'));
        }

       
            });
        
        var xmlHttp;

        function GetXmlHttpObject(){
        var xmlHttp=null;
        try{
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
        }catch (e){
        //Internet Explorer
        try{
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }catch (e){
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        }
        if(xmlHttp==null){
        alert("errore, il tuo browser non supporta ajax");
        return;
        }
        return xmlHttp;
        } 

        
        
        
